
<template>
  <el-container >
    <el-header class="no-flex" height="auto">
      <el-form inline :model="searchForm" label-width="70px">
        <el-form-item label="参数键名"  >
          <el-input v-model="searchForm.key" placeholder="请输入参数键名" :clearable="true" style="width: 200px"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="searchForm.enabled" placeholder="请选择启用状态" :clearable="true">
            <el-option v-for="item in dicts.system_global_status" :key="item.id" :label="item.k" :value="item.v"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search"  @click="upsearch">
            搜索
          </el-button>
          <el-button type="default" icon="el-icon-RefreshLeft" @click="reset">
            重置
          </el-button>
        </el-form-item>
        <el-form-item style="display: flex; margin-bottom: 0">
          <el-button type="primary" icon="el-icon-plus" @click="add" v-auths="['*:*:*','system:config:add']">
            新增
          </el-button>
        </el-form-item>
      </el-form>
    </el-header>
    <el-main class="nopadding">
      <el-table
        ref="table"
        row-key="id"
        @selection-change="selectionChange"
        :cell-style="{'text-align':'center'}"
        :header-cell-style="{'text-align':'center'}"
        hide-do :data="tableData">
        <el-table-column type="selection" width="50"></el-table-column>
        <el-table-column label="键名" prop="k"></el-table-column>
        <el-table-column label="键值" prop="v"></el-table-column>
        <el-table-column label="启用状态" prop="enabled" width="300" >
          <template #default="{row}">
            <template v-for="item in dicts.system_global_status">
              <el-tag v-if="row.enabled == item.v">{{item.k}}</el-tag>
            </template>
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" align="right" width="170">
          <template #default="scope">
            <el-button-group>
              <el-button text type="primary"    @click="table_edit(scope.row)" icon="el-icon-Edit" v-auths="['*:*:*','system:config:update']">编辑</el-button>
              <el-popconfirm title="确定删除吗？" v-if="scope.row.type == 2"  @confirm="table_del(scope.row, scope.$index)">
                <template #reference>
                  <el-button text type="primary" v-auths="['*:*:*','system:config:delete']">删除</el-button>
                </template>
              </el-popconfirm>
            </el-button-group>
          </template>
        </el-table-column>
      </el-table>
    </el-main>
    <el-footer style="display: flex;justify-content: flex-end">
      <el-pagination
        v-model:current-page="searchForm.currentPage"
        v-model:page-size="searchForm.pageSize"
        :page-sizes="[10, 20, 50, 100]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-footer>
  </el-container>
  <save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSaveSuccess" @closed="dialog.save=false"></save-dialog>

</template>

<script>
import saveDialog from './components/save.vue'
export default {
  name: 'config',
  dicts: ['system_global_status'],
  components: {
    saveDialog
  },
  data() {
    return {
      dialog: {
        save: false
      },
      total:0,
      searchForm:{
        current:1,
        size:10,
        key:'',
        enabled:null,
        type:null
      },
      selection: [],
      search: {
        keyword: null
      },
      tableData:[],
      enabled:null,
    }
  },
  created() {
    this.getData()
  },
  methods: {
    // 请求数据
    async getData(){
      let res = await this.$API.system.config.page.get(this.searchForm)
        this.tableData = res.data.records;
        this.total = res.data.total;
    },
    //切换分页
    handleCurrentChange(e){
      this.searchForm.current = e;
      this.getData()
    },
    // 改变单页显示数量
    handleSizeChange(e){
      this.searchForm.size = e
      this.pagi.pageSize =e
      this.getData()
    },
    // 转换时间
    rendererDateTime(row, column, cellValue, index){
      return this.$TOOL.dateFormat(cellValue)
    },
    //添加
    add(){
      this.dialog.save = true
      this.$nextTick(() => {
        this.$refs.saveDialog.open('add').setData()
      })
    },
    //编辑
    table_edit(row){
      // console.log(row)
      this.dialog.save = true
      this.$nextTick(() => {
        this.$refs.saveDialog.open('edit').setData(row)
      })
    },
    //删除
    async table_del(row){
      let res = await this.$API.system.config.del.get(row.id)
      if(res.code == 200){
        this.$message.success('删除成功')
        this.tableData = []
        this.getData()
      } else{
        this.$message.error('删除失败')
      }
    },
    //表格选择后回调事件
    selectionChange(selection){
      this.selection = selection;
    },
    //搜索
    async upsearch(){
      this.getData()
    },
    //重置功能
    reset(){
      this.searchForm={
        current:1,
        size:10,
        key:'',
        enabled:null,
        type:null
      }
      this.tableData=[];
      this.getData()
    },
    //本地更新数据
    handleSaveSuccess(data, mode){
      if(mode =='add'){
        this.tableData = []
        this.getData()
      }else if(mode =='edit'){
        this.tableData=[]
        this.getData()
      }
    }
  }
}
</script>

<style scoped>

</style>
